// mypage/teamBill/teamBill.js
var util=require("../../utils/util");
var other=require("../../utils/other");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    year: 2020,
    month: 8,
    value:[0,0],
    time_list: '',
    timeStar: '',
    timeEnd: '',
    orders: [],
    currentPage: 1,
    pageSize: 20,
    total: 0,
    pop_show: true,
    show_none: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var month=new Date().getMonth()+1,year=new Date().getFullYear(),obj = other.dateTimePicker(2018,year);
    var timeStar = new Date(year, month-1, 1).getTime(),timeEnd = new Date(year, month, 1).getTime();
    // console.log(util.formatTime(new Date(timeStar)),util.formatTime(new Date(timeEnd)))
    that.setData({year:year,month:month>=10?month:'0'+month,time_list:obj.dateTimeArray,timeStar:timeStar,timeEnd:timeEnd})
    that.now_time(obj);
    that.commission_order();
  },
  // 当前时间
  now_time:function(obj){
    var that=this,year=that.data.year,month=that.data.month,value=that.data.value;
    var time=obj.dateTimeArray;
    for(var i=0;i<time[0].length;i++){
      if(time[0][i]==year){
        value[0]=i
      }
    }
    for(var j=0;j<time[1].length;j++){
      if(time[1][j]==month){
        value[1]=j
      }
    }
    that.setData({value:value});
  },
  // 弹窗
  close_pop:function(){
    this.setData({pop_show:!this.data.pop_show})
  },
  open_pop:function(e){
    this.setData({pop_show:!this.data.pop_show})
  },
  // 选择时间
  picker_change_time:function(e){
    var that = this;
    var val = e.detail.value;
    that.setData({value: val})
  },
  confirm_time:function(){
    var that=this,time_list=that.data.time_list,value=that.data.value;
    var year=time_list[0][value[0]],month=Number(time_list[1][value[1]].replace(/^0/, ''));
    var timeStar = new Date(year, month-1, 1).getTime(),timeEnd = new Date(year, month, 1).getTime();
    that.setData({timeStar:timeStar,timeEnd:timeEnd,currentPage:1,table_list:[],year:year,month:time_list[1][value[1]],show_none:false,orders:[]})
    that.close_pop();
    that.commission_order();
  },
  // 触底
  onReachBottom:function(){
    var that=this,total=that.data.total,orders=that.data.orders;
    if(orders.length<total){
      that.setData({currentPage:that.data.currentPage+1})
      that.commission_order()
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  return:function(){},
  commission_order(){
    wx.showLoading()
    var that=this,token=wx.getStorageSync('token'),orders=that.data.orders;
    var params={
      currentPage: that.data.currentPage,
      pageSize: that.data.pageSize,
      startTime: that.data.timeStar,
      endTime: that.data.timeEnd
    }
    my.get_commission_order(params,token).then(res=>{
      wx.hideLoading()
      var {total,records}=res.data.data
      records.forEach(item=>{
        item.commission=item.commission?util.toDecimal(item.commission):0
        item.updateTime=item.updateTime?util.formatTime(new Date(item.updateTime)):util.formatTime(new Date())
      })
      // console.log(records)
      orders.push(...records)
      that.setData({total:total,orders:orders})
      if(orders.length==0&&records.length==0){
        that.setData({show_none:true})
      }
    })
  },
})